package org.wu.framework.authorization.platform.infrastructure.persistence;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.wu.framework.authorization.platform.infrastructure.converter.SysMenuConverter;
import org.wu.framework.authorization.platform.infrastructure.entity.SysMenuDO;
import org.wu.framework.authorization.platform.domain.model.sys.menu.SysMenu;
import org.wu.framework.authorization.platform.domain.model.sys.menu.SysMenuRepository;
import org.wu.framework.authorization.platform.infrastructure.entity.SysPermissionDO;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazySetValueWrappers;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.lazy.orm.web.plus.AbstractLazyCrudRepository;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysMenuRepositoryImpl extends AbstractLazyCrudRepository<SysMenuDO, SysMenu, Integer> implements SysMenuRepository {

    @Autowired
    LazyLambdaStream lazyLambdaStream;


    /**
     * describe 新增
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysMenu> story(SysMenu sysMenu) {
        SysMenuDO sysMenuDO = SysMenuConverter.fromMenu(sysMenu);
        lazyLambdaStream.upsert(sysMenuDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysMenu> findOne(SysMenu sysMenu) {
        SysMenuDO sysMenuDO = SysMenuConverter.fromMenu(sysMenu);
        SysMenu sysMenuOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(sysMenuDO), SysMenu.class);
        return ResultFactory.successOf(sysMenuOne);
    }

    /**
     * describe 查询多个
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<List<SysMenu>> findList(SysMenu sysMenu) {
        SysMenuDO sysMenuDO = SysMenuConverter.fromMenu(sysMenu);
        List<SysMenu> sysMenuList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(sysMenuDO), SysMenu.class);
        return ResultFactory.successOf(sysMenuList);
    }

    /**
     * describe 删除
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysMenu> delete(SysMenu sysMenu) {
        SysMenuDO sysMenuDO = SysMenuConverter.fromMenu(sysMenu);
        lazyLambdaStream.delete(LazyWrappers.<SysMenuDO>lambdaWrapperBean(sysMenuDO));
        return ResultFactory.successOf();
    }

    /**
     * 批量存储
     *
     * @param sysMenuList
     * @return
     */
    @Override
    public Result batchStory(List<SysMenu> sysMenuList) {
        List<SysMenuDO> sysMenuDOList = sysMenuList.stream().map(SysMenuConverter::fromMenu).collect(Collectors.toList());
        lazyLambdaStream.upsert(sysMenuDOList);
        return ResultFactory.successOf();
    }

    /**
     * 删除所有菜单
     *
     * @param ids
     * @return
     */
    @Override
    public Result<SysMenu> deleteByIds(List<Integer> ids) {
        lazyLambdaStream.update(
                LazySetValueWrappers.<SysMenuDO>lambdaWrapper()
                        .set(SysMenuDO::getIsDeleted, true),
                LazyWrappers.<SysMenuDO>lambdaWrapper()
                        .inIgnoreEmpty(SysMenuDO::getId, ids)
                        .eqIgnoreEmpty(SysMenuDO::getIsDeleted, false)
        );
        return ResultFactory.successOf();
    }
}